package dshop.model.activity;

import java.util.Date;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.BaseModel;
import dshop.AppConst;

public class Activity extends BaseModel<Activity>{
	
	private static final long serialVersionUID = -6967680902549117464L;
	
	public static final Activity dao = new Activity();

	public static void delete(int activityId) 
	{
		new Activity().set("is_delete", AppConst.DELETE_STR_YES)
		 		 	 .set("delete_date_time", new Date())
		 		 	 .set("id", activityId)
		 		 	 .update();
	}

	public static void setCheckStatus(int activityId, String checkStatus, String checkStatusExplain) 
	{
		new Activity().set("check_status", checkStatus)
		 		 	 .set("check_status_explain", checkStatusExplain)
		 		 	 .set("check_date_time", new Date())
		 		 	 .set("id", activityId)
		 		 	 .update();            
	}
	public static void setCheckStatusFail(int activityId, String checkStatusExplain) 
	{
		new Activity().set("check_status", AppConst.CHECK_STATUS_FAIL)
		 		 	 .set("check_status_explain", checkStatusExplain)
		 		 	 .set("sold_out", 1)
		 		 	 .set("check_date_time", new Date())
		 		 	 .set("id", activityId)
		 		 	 .update();            
	}
	//通过手机号查出用户id
	public Activity findIdByPhone(String phone)
	{
		String sql = "SELECT account_id FROM `member_general_info` where phone = ?";
		return findFirst(sql, phone);
	}
	
	//是否送过
	public Activity checkGiveToo(int userId)
	{
		String sql = "SELECT count(1) 'count' FROM `push_num` where userId = ?";
		return findFirst(sql, userId);
	}
	
	
	//二次赠送即时通次数
	public void updateJst(int userId, int num)
	{
		String sql = "UPDATE `push_num` SET `num` = (num+ ? ) WHERE `userId` = ?";
		Db.update(sql, num, userId);
	}
	
	//首次赠送即时通次数
	public void insertJst(int userId, int num)
	{
		Record userRole = new Record().set("userId", userId).set("num", num);
		String tableName = "push_num";
		Db.save(tableName, userRole);
	}
	
	//赠送即时通总量
	public void pushPond(int num)
	{
		String sql = "UPDATE `push_setting` SET `pushPond` = (pushPond+ ? )";
		Db.update(sql, num);
	}
	
	//赠送即时通总量
	public static String queryPushPond()
	{
		Record r = Db.findById("push_setting", "1");
		String pushNum = r.get("pushPond");
		return pushNum;
	}

	
}
